package mcquay

import (
	"encoding/json"
	"flag"
	"fmt"
	"github.com/simonvetter/modbus"
	"math"
	"time"
	"toyota-client1/app/socket"
	"toyota-client1/init/global"
	j_log "toyota-client1/init/j-log"
	"toyota-client1/model"
	"toyota-client1/utils/tools"
)

const NAME = "mcquay"

var Client *modbus.ModbusClient

func Run() {
	defer func() {
		time.Sleep(3 * time.Second)
		Run()
	}()

	addr := global.AppConfig.Section(NAME).Key("addr").String()
	protocol := global.AppConfig.Section(NAME).Key("protocol").String()
	portName := global.AppConfig.Section(NAME).Key("port_name").String()
	baudRate, _ := global.AppConfig.Section(NAME).Key("baud_rate").Int()
	dataBits, _ := global.AppConfig.Section(NAME).Key("data_bits").Int()
	stopBits, _ := global.AppConfig.Section(NAME).Key("stop_bits").Int()
	parityMode, _ := global.AppConfig.Section(NAME).Key("parity_mode").Int()
	scanRate, _ := global.AppConfig.Section(NAME).Key("scan_rate").Int()
	slaveID, _ := global.AppConfig.Section(NAME).Key("slave_id").Int()
	moduleID, _ := global.AppConfig.Section(NAME).Key("module_id").Int()
	slaveEquipNum, _ := global.AppConfig.Section(NAME).Key("slave_equip_num").Int()
	flag.Parse()
	if addr == "" {
		j_log.LogError([]interface{}{fmt.Sprintf("【%s】 addr cant bt empty", NAME)})
		return
	}
	if protocol == "" {
		j_log.LogError([]interface{}{fmt.Sprintf("【%s】 protocol cant bt empty", NAME)})
		return
	}
	if scanRate <= 0 {
		scanRate = 1000
	}
	if protocol != "rtu" && protocol != "tcp" {
		j_log.LogError([]interface{}{fmt.Sprintf("【%s】 protocol must be one of RTU, TCP", NAME)})
		return
	}
	var err error
	url := ""
	if protocol == "rtu" {
		url = protocol + "://" + portName
		j_log.LogInfo([]interface{}{fmt.Sprintf("【%s】 is connecting to %s", NAME, url)})
		switch uint(parityMode) {
		case modbus.PARITY_NONE:
		case modbus.PARITY_EVEN:
		case modbus.PARITY_ODD:
		default:
			j_log.LogError([]interface{}{fmt.Sprintf("【%s】 modbus rtu奇偶校验只能是 [0,1,2]", NAME)})
			return
		}
		Client, err = modbus.NewClient(&modbus.ClientConfiguration{
			URL:      url,
			Speed:    uint(baudRate),   // default
			DataBits: uint(dataBits),   // default, optional
			Parity:   uint(parityMode), // default, optional
			StopBits: uint(stopBits),   // default if no parity, optional
			Timeout:  300 * time.Millisecond,
		})
	} else {
		url = protocol + "://" + addr
		j_log.LogInfo([]interface{}{fmt.Sprintf("【%s】 is connecting to %s", NAME, url)})
		Client, err = modbus.NewClient(&modbus.ClientConfiguration{
			URL:     url,
			Timeout: 1 * time.Second,
		})
	}
	if err != nil {
		j_log.LogError([]interface{}{fmt.Sprintf("【%s】 modbus.NewClient error ", NAME)})
		return
	}
	err = Client.Open()
	if err != nil {
		j_log.LogError([]interface{}{fmt.Sprintf("【%s】 client.Open error ", NAME)})
		return
	}
	defer Client.Close()
	j_log.LogInfo([]interface{}{fmt.Sprintf("【%s】%s", NAME, "modbus连接成功")})
	// 开始循环循环读
	for {
		/*--- 机组一 ---*/
		err0 := Client.SetUnitId(uint8(slaveID))
		if err0 != nil {
			j_log.LogWarning([]interface{}{fmt.Sprintf("【%s】SetUnitId error ", NAME)})
			return
		}
		buffer, err2 := Client.ReadRegisters(2000, 33, modbus.INPUT_REGISTER)
		if err2 != nil {
			j_log.LogWarning([]interface{}{fmt.Sprintf("【%s】ReadRegisters D32000 error %v", NAME, err2.Error())})
			return
		}

		Data := &model.McQuayRaw{}
		// #主机
		Data.SlaveID = uint8(slaveID)
		D32001Bits := Uint16ToBinaryArray(buffer[0])
		Data.D32001Bit0 = uint8(D32001Bits[0])
		Data.D32001Bit1 = uint8(D32001Bits[1])
		Data.D32001Bit2 = uint8(D32001Bits[2])
		Data.D32001Bit3 = uint8(D32001Bits[3])
		Data.D32001Bit4 = uint8(D32001Bits[4])
		Data.D32001Bit5 = uint8(D32001Bits[5])
		Data.D32001Bit6 = uint8(D32001Bits[6])
		Data.D32001Bit7 = uint8(D32001Bits[7])
		Data.D32001Bit8 = uint8(D32001Bits[8])
		Data.D32001Bit9 = uint8(D32001Bits[9])
		Data.D32001Bit10 = uint8(D32001Bits[10])
		Data.D32001Bit11 = uint8(D32001Bits[11])
		Data.D32001Bit12 = uint8(D32001Bits[12])
		Data.D32001Bit13 = uint8(D32001Bits[13])
		Data.D32001Bit14 = uint8(D32001Bits[14])
		Data.D32001Bit15 = uint8(D32001Bits[15])

		D32002Bits := Uint16ToBinaryArray(buffer[1])
		Data.D32002Bit0 = uint8(D32002Bits[0])
		Data.D32002Bit1 = uint8(D32002Bits[1])
		Data.D32002Bit2 = uint8(D32002Bits[2])
		Data.D32002Bit3 = uint8(D32002Bits[3])
		Data.D32002Bit4 = uint8(D32002Bits[4])
		Data.D32002Bit5 = uint8(D32002Bits[5])
		Data.D32002Bit6 = uint8(D32002Bits[6])
		Data.D32002Bit7 = uint8(D32002Bits[7])
		Data.D32002Bit8 = uint8(D32002Bits[8])
		Data.D32002Bit9 = uint8(D32002Bits[9])
		Data.D32002Bit10 = uint8(D32002Bits[10])
		Data.D32002Bit11 = uint8(D32002Bits[11])
		Data.D32002Bit12 = uint8(D32002Bits[12])
		Data.D32002Bit13 = uint8(D32002Bits[13])
		Data.D32002Bit14 = uint8(D32002Bits[14])
		Data.D32002Bit15 = uint8(D32002Bits[15])

		D32003Bits := Uint16ToBinaryArray(buffer[2])
		Data.D32003Bit0 = uint8(D32003Bits[0])
		Data.D32003Bit1 = uint8(D32003Bits[1])
		Data.D32003Bit2 = uint8(D32003Bits[2])
		Data.D32003Bit3 = uint8(D32003Bits[3])
		Data.D32003Bit4 = uint8(D32003Bits[4])
		Data.D32003Bit5 = uint8(D32003Bits[5])
		Data.D32003Bit6 = uint8(D32003Bits[6])
		Data.D32003Bit7 = uint8(D32003Bits[7])
		Data.D32003Bit8 = uint8(D32003Bits[8])
		Data.D32003Bit9 = uint8(D32003Bits[9])
		Data.D32003Bit10 = uint8(D32003Bits[10])
		Data.D32003Bit11 = uint8(D32003Bits[11])
		Data.D32003Bit12 = uint8(D32003Bits[12])
		Data.D32003Bit13 = uint8(D32003Bits[13])
		Data.D32003Bit14 = uint8(D32003Bits[14])
		Data.D32003Bit15 = uint8(D32003Bits[15])
		Data.D32004 = RealValue(buffer[3], 1, false)
		Data.D32005 = RealValue(buffer[4], 1, false)
		Data.D32006 = RealValue(buffer[5], 1, false)
		D32007Bits := Uint16ToBinaryArray(buffer[6])
		Data.D32007Bit8 = uint8(D32007Bits[8])
		Data.D32007Bit9 = uint8(D32007Bits[9])
		Data.D32007Bit10 = uint8(D32007Bits[10])
		Data.D32007Bit11 = uint8(D32007Bits[11])
		Data.D32008 = RealValue(buffer[7], 1, false)

		// 0#从机
		D32012Bits := Uint16ToBinaryArray(buffer[11])
		Data.D32012Bit0 = uint8(D32012Bits[0])
		Data.D32012Bit1 = uint8(D32012Bits[1])
		Data.D32012Bit2 = uint8(D32012Bits[2])
		Data.D32012Bit3 = uint8(D32012Bits[3])
		Data.D32012Bit4 = uint8(D32012Bits[4])
		Data.D32012Bit5 = uint8(D32012Bits[5])
		Data.D32012Bit6 = uint8(D32012Bits[6])
		Data.D32012Bit7 = uint8(D32012Bits[7])
		Data.D32012Bit8 = uint8(D32012Bits[8])
		Data.D32012Bit9 = uint8(D32012Bits[9])
		Data.D32012Bit10 = uint8(D32012Bits[10])
		Data.D32012Bit11 = uint8(D32012Bits[11])
		Data.D32012Bit12 = uint8(D32012Bits[12])
		Data.D32012Bit13 = uint8(D32012Bits[13])
		Data.D32012Bit14 = uint8(D32012Bits[14])
		Data.D32012Bit15 = uint8(D32012Bits[15])

		D32013Bits := Uint16ToBinaryArray(buffer[12])
		Data.D32013Bit0 = uint8(D32013Bits[0])
		Data.D32013Bit1 = uint8(D32013Bits[1])
		Data.D32013Bit2 = uint8(D32013Bits[2])
		Data.D32013Bit3 = uint8(D32013Bits[3])
		Data.D32013Bit4 = uint8(D32013Bits[4])
		Data.D32013Bit5 = uint8(D32013Bits[5])
		Data.D32013Bit6 = uint8(D32013Bits[6])
		Data.D32013Bit7 = uint8(D32013Bits[7])
		Data.D32013Bit8 = uint8(D32013Bits[8])
		Data.D32013Bit9 = uint8(D32013Bits[9])
		Data.D32013Bit10 = uint8(D32013Bits[10])
		Data.D32013Bit11 = uint8(D32013Bits[11])
		Data.D32013Bit12 = uint8(D32013Bits[12])
		Data.D32013Bit13 = uint8(D32013Bits[13])
		Data.D32013Bit14 = uint8(D32013Bits[14])
		Data.D32013Bit15 = uint8(D32013Bits[15])

		D32014Bits := Uint16ToBinaryArray(buffer[13])
		Data.D32014Bit0 = uint8(D32014Bits[0])
		Data.D32014Bit1 = uint8(D32014Bits[1])
		Data.D32014Bit2 = uint8(D32014Bits[2])
		Data.D32014Bit3 = uint8(D32014Bits[3])
		Data.D32014Bit4 = uint8(D32014Bits[4])
		Data.D32014Bit5 = uint8(D32014Bits[5])
		Data.D32014Bit6 = uint8(D32014Bits[6])
		Data.D32014Bit7 = uint8(D32014Bits[7])
		Data.D32014Bit8 = uint8(D32014Bits[8])
		Data.D32014Bit9 = uint8(D32014Bits[9])
		Data.D32014Bit10 = uint8(D32014Bits[10])
		Data.D32014Bit11 = uint8(D32014Bits[11])
		Data.D32014Bit12 = uint8(D32014Bits[12])
		Data.D32014Bit13 = uint8(D32014Bits[13])
		Data.D32014Bit14 = uint8(D32014Bits[14])
		Data.D32014Bit15 = uint8(D32014Bits[15])
		Data.D32015 = RealValue(buffer[14], 1, false)
		Data.D32016 = RealValue(buffer[15], 1, false)
		Data.D32017 = RealValue(buffer[16], 1, false)
		D32018Bits := Uint16ToBinaryArray(buffer[17])
		Data.D32018Bit8 = uint8(D32018Bits[8])
		Data.D32018Bit9 = uint8(D32018Bits[9])
		Data.D32018Bit10 = uint8(D32018Bits[10])
		Data.D32018Bit11 = uint8(D32018Bits[11])
		Data.D32019 = RealValue(buffer[18], 1, false)

		if slaveEquipNum >= 2 {
			// 1#从机
			D32023Bits := Uint16ToBinaryArray(buffer[22])
			Data.D32023Bit0 = uint8(D32023Bits[0])
			Data.D32023Bit1 = uint8(D32023Bits[1])
			Data.D32023Bit2 = uint8(D32023Bits[2])
			Data.D32023Bit3 = uint8(D32023Bits[3])
			Data.D32023Bit4 = uint8(D32023Bits[4])
			Data.D32023Bit5 = uint8(D32023Bits[5])
			Data.D32023Bit6 = uint8(D32023Bits[6])
			Data.D32023Bit7 = uint8(D32023Bits[7])
			Data.D32023Bit8 = uint8(D32023Bits[8])
			Data.D32023Bit9 = uint8(D32023Bits[9])
			Data.D32023Bit10 = uint8(D32023Bits[10])
			Data.D32023Bit11 = uint8(D32023Bits[11])
			Data.D32023Bit12 = uint8(D32023Bits[12])
			Data.D32023Bit13 = uint8(D32023Bits[13])
			Data.D32023Bit14 = uint8(D32023Bits[14])
			Data.D32023Bit15 = uint8(D32023Bits[15])

			D32024Bits := Uint16ToBinaryArray(buffer[23])
			Data.D32024Bit0 = uint8(D32024Bits[0])
			Data.D32024Bit1 = uint8(D32024Bits[1])
			Data.D32024Bit2 = uint8(D32024Bits[2])
			Data.D32024Bit3 = uint8(D32024Bits[3])
			Data.D32024Bit4 = uint8(D32024Bits[4])
			Data.D32024Bit5 = uint8(D32024Bits[5])
			Data.D32024Bit6 = uint8(D32024Bits[6])
			Data.D32024Bit7 = uint8(D32024Bits[7])
			Data.D32024Bit8 = uint8(D32024Bits[8])
			Data.D32024Bit9 = uint8(D32024Bits[9])
			Data.D32024Bit10 = uint8(D32024Bits[10])
			Data.D32024Bit11 = uint8(D32024Bits[11])
			Data.D32024Bit12 = uint8(D32024Bits[12])
			Data.D32024Bit13 = uint8(D32024Bits[13])
			Data.D32024Bit14 = uint8(D32024Bits[14])
			Data.D32024Bit15 = uint8(D32024Bits[15])

			D32025Bits := Uint16ToBinaryArray(buffer[24])
			Data.D32025Bit0 = uint8(D32025Bits[0])
			Data.D32025Bit1 = uint8(D32025Bits[1])
			Data.D32025Bit2 = uint8(D32025Bits[2])
			Data.D32025Bit3 = uint8(D32025Bits[3])
			Data.D32025Bit4 = uint8(D32025Bits[4])
			Data.D32025Bit5 = uint8(D32025Bits[5])
			Data.D32025Bit6 = uint8(D32025Bits[6])
			Data.D32025Bit7 = uint8(D32025Bits[7])
			Data.D32025Bit8 = uint8(D32025Bits[8])
			Data.D32025Bit9 = uint8(D32025Bits[9])
			Data.D32025Bit10 = uint8(D32025Bits[10])
			Data.D32025Bit11 = uint8(D32025Bits[11])
			Data.D32025Bit12 = uint8(D32025Bits[12])
			Data.D32025Bit13 = uint8(D32025Bits[13])
			Data.D32025Bit14 = uint8(D32025Bits[14])
			Data.D32025Bit15 = uint8(D32025Bits[15])
			Data.D32026 = RealValue(buffer[25], 1, false)
			Data.D32027 = RealValue(buffer[26], 1, false)
			Data.D32028 = RealValue(buffer[27], 1, false)
			D32029Bits := Uint16ToBinaryArray(buffer[28])
			Data.D32029Bit8 = uint8(D32029Bits[8])
			Data.D32029Bit9 = uint8(D32029Bits[9])
			Data.D32029Bit10 = uint8(D32029Bits[10])
			Data.D32029Bit11 = uint8(D32029Bits[11])
			Data.D32030 = RealValue(buffer[29], 1, false)
		}

		buffer, err2 = Client.ReadRegisters(1299, 60, modbus.INPUT_REGISTER)
		if err2 != nil {
			j_log.LogWarning([]interface{}{fmt.Sprintf("【%s】ReadRegisters D31300 error %v", NAME, err2.Error())})
			return
		}
		// #主机
		Data.D31300HighByte, _ = tools.Uint16BitsToUint8(buffer[0], 8, 15, "big")
		Data.D31300LowByte, _ = tools.Uint16BitsToUint8(buffer[0], 0, 7, "big")
		Data.D31301HighByte, _ = tools.Uint16BitsToUint8(buffer[1], 8, 15, "big")
		Data.D31301LowByte, _ = tools.Uint16BitsToUint8(buffer[1], 0, 7, "big")

		D31308Bits := Uint16ToBinaryArray(buffer[8])
		Data.D31308Bit0 = uint8(D31308Bits[0])
		Data.D31308Bit1 = uint8(D31308Bits[1])
		Data.D31308Bit2 = uint8(D31308Bits[2])
		Data.D31308Bit3 = uint8(D31308Bits[3])
		Data.D31308Bit4 = uint8(D31308Bits[4])
		Data.D31308Bit5 = uint8(D31308Bits[5])
		Data.D31308Bit6 = uint8(D31308Bits[6])

		D31309Bits := Uint16ToBinaryArray(buffer[9])
		Data.D31309Bit0 = uint8(D31309Bits[0])
		Data.D31309Bit1 = uint8(D31309Bits[1])
		Data.D31309Bit2 = uint8(D31309Bits[2])
		Data.D31309Bit3 = uint8(D31309Bits[3])
		Data.D31309Bit4 = uint8(D31309Bits[4])
		Data.D31309Bit5 = uint8(D31309Bits[5])
		Data.D31309Bit6 = uint8(D31309Bits[6])

		D31310Bits := Uint16ToBinaryArray(buffer[10])
		Data.D31310Bit0 = uint8(D31310Bits[0])
		Data.D31310Bit1 = uint8(D31310Bits[1])
		Data.D31310Bit2 = uint8(D31310Bits[2])
		Data.D31310Bit3 = uint8(D31310Bits[3])
		Data.D31310Bit4 = uint8(D31310Bits[4])
		Data.D31310Bit5 = uint8(D31310Bits[5])

		// 0#从机
		Data.D31320HighByte, _ = tools.Uint16BitsToUint8(buffer[20], 8, 15, "big")
		Data.D31320LowByte, _ = tools.Uint16BitsToUint8(buffer[20], 0, 7, "big")
		Data.D31321HighByte, _ = tools.Uint16BitsToUint8(buffer[21], 8, 15, "big")
		Data.D31321LowByte, _ = tools.Uint16BitsToUint8(buffer[21], 0, 7, "big")

		D31328Bits := Uint16ToBinaryArray(buffer[28])
		Data.D31328Bit0 = uint8(D31328Bits[0])
		Data.D31328Bit1 = uint8(D31328Bits[1])
		Data.D31328Bit2 = uint8(D31328Bits[2])
		Data.D31328Bit3 = uint8(D31328Bits[3])
		Data.D31328Bit4 = uint8(D31328Bits[4])
		Data.D31328Bit5 = uint8(D31328Bits[5])
		Data.D31328Bit6 = uint8(D31328Bits[6])

		D31329Bits := Uint16ToBinaryArray(buffer[29])
		Data.D31329Bit0 = uint8(D31329Bits[0])
		Data.D31329Bit1 = uint8(D31329Bits[1])
		Data.D31329Bit2 = uint8(D31329Bits[2])
		Data.D31329Bit3 = uint8(D31329Bits[3])
		Data.D31329Bit4 = uint8(D31329Bits[4])
		Data.D31329Bit5 = uint8(D31329Bits[5])
		Data.D31329Bit6 = uint8(D31329Bits[6])

		D31330Bits := Uint16ToBinaryArray(buffer[30])
		Data.D31330Bit0 = uint8(D31330Bits[0])
		Data.D31330Bit1 = uint8(D31330Bits[1])
		Data.D31330Bit2 = uint8(D31330Bits[2])
		Data.D31330Bit3 = uint8(D31330Bits[3])
		Data.D31330Bit4 = uint8(D31330Bits[4])
		Data.D31330Bit5 = uint8(D31330Bits[5])
		if slaveEquipNum >= 2 {
			// 1#从机
			Data.D31340HighByte, _ = tools.Uint16BitsToUint8(buffer[40], 8, 15, "big")
			Data.D31340LowByte, _ = tools.Uint16BitsToUint8(buffer[40], 0, 7, "big")
			Data.D31341HighByte, _ = tools.Uint16BitsToUint8(buffer[41], 8, 15, "big")
			Data.D31341LowByte, _ = tools.Uint16BitsToUint8(buffer[41], 0, 7, "big")

			D31348Bits := Uint16ToBinaryArray(buffer[48])
			Data.D31348Bit0 = uint8(D31348Bits[0])
			Data.D31348Bit1 = uint8(D31348Bits[1])
			Data.D31348Bit2 = uint8(D31348Bits[2])
			Data.D31348Bit3 = uint8(D31348Bits[3])
			Data.D31348Bit4 = uint8(D31348Bits[4])
			Data.D31348Bit5 = uint8(D31348Bits[5])
			Data.D31348Bit6 = uint8(D31348Bits[6])

			D31349Bits := Uint16ToBinaryArray(buffer[49])
			Data.D31349Bit0 = uint8(D31349Bits[0])
			Data.D31349Bit1 = uint8(D31349Bits[1])
			Data.D31349Bit2 = uint8(D31349Bits[2])
			Data.D31349Bit3 = uint8(D31349Bits[3])
			Data.D31349Bit4 = uint8(D31349Bits[4])
			Data.D31349Bit5 = uint8(D31349Bits[5])
			Data.D31349Bit6 = uint8(D31349Bits[6])

			D31350Bits := Uint16ToBinaryArray(buffer[50])
			Data.D31350Bit0 = uint8(D31350Bits[0])
			Data.D31350Bit1 = uint8(D31350Bits[1])
			Data.D31350Bit2 = uint8(D31350Bits[2])
			Data.D31350Bit3 = uint8(D31350Bits[3])
			Data.D31350Bit4 = uint8(D31350Bits[4])
			Data.D31350Bit5 = uint8(D31350Bits[5])
		}

		buffer, err2 = Client.ReadRegisters(4999, 25, modbus.INPUT_REGISTER)
		if err2 != nil {
			j_log.LogWarning([]interface{}{fmt.Sprintf("【%s】ReadRegisters D35000 error %v", NAME, err2.Error())})
			return
		}

		Data.D35000 = buffer[0]
		Data.D35001 = buffer[1]
		Data.D35002 = RealValue(buffer[2], 1, false)
		Data.D35003 = RealValue(buffer[3], 1, false)

		D35004Bits := Uint16ToBinaryArray(buffer[4])
		Data.D35004Bit0 = uint8(D35004Bits[0])
		Data.D35004Bit1 = uint8(D35004Bits[1])
		Data.D35004Bit2 = uint8(D35004Bits[2])
		Data.D35004Bit3 = uint8(D35004Bits[3])
		Data.D35004Bit4 = uint8(D35004Bits[4])
		Data.D35004Bit5 = uint8(D35004Bits[5])
		Data.D35004Bit6 = uint8(D35004Bits[6])
		Data.D35004Bit7 = uint8(D35004Bits[7])
		Data.D35004Bit8 = uint8(D35004Bits[8])
		Data.D35004Bit9 = uint8(D35004Bits[9])
		Data.D35004Bit10 = uint8(D35004Bits[10])
		Data.D35004Bit11 = uint8(D35004Bits[11])
		Data.D35004Bit12 = uint8(D35004Bits[12])

		Data.D35005 = RealValue(buffer[5], 1, false)
		Data.D35006 = RealValue(buffer[6], 1, false)
		Data.D35007 = RealValue(buffer[7], 1, false)

		D35008Bits := Uint16ToBinaryArray(buffer[8])
		Data.D35008Bit0 = uint8(D35008Bits[0])
		Data.D35008Bit1 = uint8(D35008Bits[1])
		Data.D35008Bit2 = uint8(D35008Bits[2])
		Data.D35008Bit3 = uint8(D35008Bits[3])
		Data.D35008Bit4 = uint8(D35008Bits[4])
		Data.D35008Bit5 = uint8(D35008Bits[5])
		Data.D35008Bit6 = uint8(D35008Bits[6])
		Data.D35008Bit7 = uint8(D35008Bits[7])
		Data.D35008Bit8 = uint8(D35008Bits[8])
		Data.D35008Bit9 = uint8(D35008Bits[9])
		Data.D35008Bit10 = uint8(D35008Bits[10])
		Data.D35008Bit11 = uint8(D35008Bits[11])
		Data.D35008Bit12 = uint8(D35008Bits[12])
		Data.D35008Bit13 = uint8(D35008Bits[13])
		Data.D35008Bit14 = uint8(D35008Bits[14])

		D35009Bits := Uint16ToBinaryArray(buffer[9])
		Data.D35009Bit0 = uint8(D35009Bits[0])
		Data.D35009Bit1 = uint8(D35009Bits[1])
		Data.D35009Bit2 = uint8(D35009Bits[2])
		Data.D35009Bit3 = uint8(D35009Bits[3])
		Data.D35009Bit4 = uint8(D35009Bits[4])
		Data.D35009Bit5 = uint8(D35009Bits[5])
		Data.D35009Bit6 = uint8(D35009Bits[6])
		Data.D35009Bit7 = uint8(D35009Bits[7])
		Data.D35009Bit8 = uint8(D35009Bits[8])
		Data.D35009Bit9 = uint8(D35009Bits[9])
		Data.D35009Bit10 = uint8(D35009Bits[10])

		Data.D35010 = buffer[10]
		Data.D35011 = buffer[11]
		Data.D35012 = buffer[12]
		Data.D35013 = RealValue(buffer[13], 1, false)

		D35014Bits := Uint16ToBinaryArray(buffer[14])
		Data.D35014Bit0 = uint8(D35014Bits[0])
		Data.D35014Bit1 = uint8(D35014Bits[1])
		Data.D35014Bit2 = uint8(D35014Bits[2])
		Data.D35014Bit3 = uint8(D35014Bits[3])
		Data.D35014Bit4 = uint8(D35014Bits[4])

		Data.D35015 = buffer[15]
		Data.D35016 = buffer[16]
		Data.D35017 = buffer[17]
		Data.D35018 = buffer[18]
		Data.D35019 = buffer[19]
		Data.D35020 = buffer[20]

		Data.D35021Bit0_1, _ = tools.Uint16BitsToUint8(buffer[21], 0, 1, "big")
		Data.D35021Bit2_3, _ = tools.Uint16BitsToUint8(buffer[21], 2, 3, "big")
		Data.D35021Bit4_5, _ = tools.Uint16BitsToUint8(buffer[21], 4, 5, "big")
		Data.D35021Bit8_12, _ = tools.Uint16BitsToUint8(buffer[21], 8, 12, "big")

		Data.D35022 = RealValue(buffer[22], 1, false)
		Data.D35023 = buffer[23]

		Data.D35024Bit0_7, _ = tools.Uint16BitsToUint8(buffer[24], 0, 7, "big")
		Data.D35024Bit8_11, _ = tools.Uint16BitsToUint8(buffer[24], 8, 11, "big")

		jsonB, _ := json.Marshal(Data)
		_ = socket.Client.SendMessage(moduleID, NAME, string(jsonB))
		time.Sleep(time.Duration(scanRate) * time.Millisecond)
	}
}

// Uint16ToBinaryArray 项目专用
func Uint16ToBinaryArray(b uint16) (res []int) {
	binaryString := ""
	for i := 15; i >= 0; i-- { // 从最高位到最低位
		if (b & (1 << i)) != 0 {
			binaryString += "1"
		} else {
			binaryString += "0"
		}
	}
	res = tools.BinaryStringToArray(binaryString)
	tools.SliceReverse(&res)
	return
}

// RealValue 给原始数据加上系数
func RealValue(rawData uint16, precious int, unsigned bool) (f float64) {
	f0 := float64(rawData)
	if unsigned == false {
		f0 = float64(int16(rawData))
	}
	plusV := math.Pow10(precious)
	//f = tools.RoundFloat(f0/plusV, precious)
	f = f0 / plusV
	return
}
